/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"@(#)st_bugs.c	1.5	05/06/08 SMI"

/*
 * Workarounds for stabs generation bugs in the compiler
 */

#include <stdio.h>
#include <strings.h>

#include "ctf_headers.h"
#include "ctftools.h"
#include "hash.h"
#include "memory.h"

/*
 * Due to 4432619, the 6.1 compiler will sometimes incorrectly generate pointer
 * stabs.  Given a struct foo, and a corresponding typedef struct foo foo_t.
 * In some cases, when faced with a pointer to a foo_t, the compiler will
 * sometimes generate a stab that describes a pointer to a struct foo.
 * Regardless of correctness, this breaks merges, as it occurs inconsistently
 * by file.  The following two routines know how to recognize and repair foo_t *
 * and foo_t ** bugs in a specific set of cases.  There is no general way to
 * solve this problem without a fix to the compiler.  In general, cases should
 * only be added to these routines to fix merging problems in genunix.
 */
static void
fix_ptrptr_to_struct(tdata_t *td)
{
	char *strs[2] = { "as", "fdbuffer" };
	char *mems[2] = { "a_objectdir", "fd_shadow" };
	char *acts[2] = { "vnode", "page" };
	char *tgts[2] = { "vnode_t", "page_t" };
	tdesc_t *str;
	tdesc_t *act, *tgt;
	tdesc_t *p1, *p2;
	mlist_t *ml;
	int i;

	for (i = 0; i < sizeof (strs) / sizeof (strs[0]); i++) {
		if (!(str = lookupname(strs[i])) || str->t_type != STRUCT)
			continue;

		for (ml = str->t_members; ml; ml = ml->ml_next) {
			if (streq(ml->ml_name, mems[i]))
				break;
		}
		if (!ml)
			continue;

		if (ml->ml_type->t_type != POINTER || ml->ml_type->t_name ||
		    ml->ml_type->t_tdesc->t_type != POINTER ||
		    ml->ml_type->t_tdesc->t_name)
			continue;

		act = ml->ml_type->t_tdesc->t_tdesc;
		if (act->t_type != STRUCT || !streq(act->t_name, acts[i]))
			continue;

		if (!(tgt = lookupname(tgts[i])) || tgt->t_type != TYPEDEF)
			continue;

		/* We have an instance of the bug */
		p2 = xcalloc(sizeof (*p2));
		p2->t_type = POINTER;
		p2->t_id = td->td_nextid++;
		p2->t_tdesc = tgt;

		p1 = xcalloc(sizeof (*p1));
		p1->t_type = POINTER;
		p1->t_id = td->td_nextid++;
		p1->t_tdesc = p2;

		ml->ml_type = p1;

		debug(3, "Fixed %s->%s => ptrptr struct %s bug\n",
		    strs[i], mems[i], acts[i]);
	}
}

static void
fix_ptr_to_struct(tdata_t *td)
{
	char *strs[2] = { "vmem", "id_space" };
	char *mems[2] = { NULL, "is_vmem" };
	tdesc_t *ptr = NULL;
	tdesc_t *str, *vmt;
	mlist_t *ml;
	int i;

	if ((vmt = lookupname("vmem_t")) == NULL || vmt->t_type != TYPEDEF)
		return;

	for (i = 0; i < sizeof (strs) / sizeof (strs[0]); i++) {
		if (!(str = lookupname(strs[i])) || str->t_type != STRUCT)
			continue;

		for (ml = str->t_members; ml; ml = ml->ml_next) {
			if (mems[i] && !streq(ml->ml_name, mems[i]))
				continue;

			if (ml->ml_type->t_type != POINTER ||
			    ml->ml_type->t_name ||
			    (ml->ml_type->t_tdesc->t_type != STRUCT &&
			    ml->ml_type->t_tdesc->t_type != FORWARD) ||
			    !streq(ml->ml_type->t_tdesc->t_name, "vmem"))
				continue;

			debug(3, "Fixed %s->%s => ptr struct vmem bug\n",
			    strs[i], ml->ml_name);

			if (!ptr) {
				ptr = xcalloc(sizeof (*ptr));
				ptr->t_type = POINTER;
				ptr->t_id = td->td_nextid++;
				ptr->t_tdesc = vmt;
			}

			ml->ml_type = ptr;
		}
	}
}

/*
 * The cpu structure grows, with the addition of a machcpu member, if
 * _MACHDEP is defined.  This means that, for example, the cpu structure
 * in unix is different from the cpu structure in genunix.  As one might
 * expect, this causes merges to fail.  Since everyone indirectly contains
 * a pointer to a CPU structure, the failed merges can cause massive amounts
 * of duplication.  In the case of unix uniquifying against genunix, upwards
 * of 50% of the structures were unmerged due to this problem.  We fix this
 * by adding a cpu_m member.  If machcpu hasn't been defined in our module,
 * we make a forward node for it.
 */
static void
fix_small_cpu_struct(tdata_t *td)
{
	tdesc_t *cput, *cpu;
	tdesc_t *machcpu;
	mlist_t *ml, *lml;
	mlist_t *cpum;
	int foundcpucyc = 0;
	size_t lmlsz;

	/*
	 * We're going to take the circuitous route finding the cpu structure,
	 * because we want to make sure that we find the right one.  It would
	 * be nice if we could verify the header name too.
	 */
	if ((cput = lookupname("cpu_t")) == NULL || cput->t_type != TYPEDEF)
		return;

	cpu = cput->t_tdesc;
	if (!streq(cpu->t_name, "cpu") || cpu->t_type != STRUCT)
		return;

	for (ml = cpu->t_members, lml = NULL; ml;
	    lml = ml, ml = ml->ml_next) {
		if (strcmp(ml->ml_name, "cpu_cyclic") == 0)
			foundcpucyc = 1;
	}

	if (foundcpucyc == 0 || lml == NULL ||
	    strcmp(lml->ml_name, "cpu_m") == 0)
		return;

	/*
	 * We need to find out how big the last element is so we can compute the
	 * offset of the new one.  If the last element has a size, we use it.
	 * If it doesn't, it should be a pointer, which won't have a size.  In
	 * that case, we look up the size of a long and use that as the size of
	 * the pointer.
	 */
	if (lml->ml_type->t_size)
		lmlsz = lml->ml_type->t_size * NBBY;
	else {
		tdesc_t *tdp = lookupname("long");
		lmlsz = tdp->t_intr->intr_nbits;
	}

	if ((machcpu = lookupname("machcpu")) == NULL) {
		machcpu = xcalloc(sizeof (*machcpu));
		machcpu->t_name = xstrdup("machcpu");
		machcpu->t_id = td->td_nextid++;
		machcpu->t_type = FORWARD;

	} else if (machcpu->t_type != STRUCT)
		return;

	debug(3, "Adding cpu_m machcpu %s to cpu struct\n",
	    (machcpu->t_type == FORWARD ? "forward" : "struct"));

	cpum = xmalloc(sizeof (*cpum));
	cpum->ml_offset = lml->ml_offset + lmlsz;
	cpum->ml_size = 0;
	cpum->ml_name = xstrdup("cpu_m");
	cpum->ml_type = machcpu;
	cpum->ml_next = NULL;

	lml->ml_next = cpum;
}

/*
 * Fix stabs generation bugs.  These routines must be run before the
 * post-conversion merge
 */
void
cvt_fixbugs(tdata_t *td)
{
	fix_ptrptr_to_struct(td);
	fix_ptr_to_struct(td);
	fix_small_cpu_struct(td);
}
